/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddCourseDialog.java
 *
 * Created on Apr 3, 2011, 1:16:29 AM
 */

package ohtu;

import java.util.ArrayList;

/**
 *
 * @author johannes
 */
public class AddCourseDialog extends javax.swing.JDialog {

	private ArrayList<Course> Courses;
	private Course courseToEdit;

	/** Creates new form AddCourseDialog */
	public AddCourseDialog(java.awt.Frame parent, ArrayList<Course> c, Course course) {
		super(parent, true);
		Courses = c;
		courseToEdit = course;
		initComponents();
		if(courseToEdit != null)
		{
			courseNameField.setText(courseToEdit.getName());
			creditsField.setText(Integer.toString(courseToEdit.getPoints()));
		}
		setVisible(true);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {

                courseNameLabel = new javax.swing.JLabel();
                courseNameField = new javax.swing.JTextField();
                creditsLabel = new javax.swing.JLabel();
                creditsField = new javax.swing.JTextField();
                cancelButton = new javax.swing.JButton();
                okButton = new javax.swing.JButton();

                setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
                setName("Form"); // NOI18N

                org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ohtu.OhtuApp.class).getContext().getResourceMap(AddCourseDialog.class);
                courseNameLabel.setText(resourceMap.getString("courseNameLabel.text")); // NOI18N
                courseNameLabel.setName("courseNameLabel"); // NOI18N

                courseNameField.setText(resourceMap.getString("courseNameField.text")); // NOI18N
                courseNameField.setName("courseNameField"); // NOI18N
                courseNameField.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                courseNameFieldActionPerformed(evt);
                        }
                });

                creditsLabel.setText(resourceMap.getString("creditsLabel.text")); // NOI18N
                creditsLabel.setName("creditsLabel"); // NOI18N

                creditsField.setText(resourceMap.getString("creditsField.text")); // NOI18N
                creditsField.setName("creditsField"); // NOI18N

                cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
                cancelButton.setName("cancelButton"); // NOI18N
                cancelButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                cancelButtonActionPerformed(evt);
                        }
                });

                okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
                okButton.setName("okButton"); // NOI18N
                okButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                okButtonActionPerformed(evt);
                        }
                });

                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                getContentPane().setLayout(layout);
                layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(creditsLabel)
                                        .addComponent(courseNameLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(courseNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(creditsField, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap(70, Short.MAX_VALUE)
                                .addComponent(okButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cancelButton)
                                .addContainerGap())
                );

                layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

                layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(courseNameLabel)
                                        .addComponent(courseNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(creditsLabel)
                                        .addComponent(creditsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(cancelButton)
                                        .addComponent(okButton))
                                .addContainerGap())
                );

                pack();
        }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
	    this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
	    Courses.remove(courseToEdit);
	    courseToEdit = new Course(courseNameField.getText(), 0, Integer.parseInt(creditsField.getText()) );
	    Courses.add(courseToEdit);
	    this.dispose();
    }//GEN-LAST:event_okButtonActionPerformed

    private void courseNameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_courseNameFieldActionPerformed
	    // TODO add your handling code here:
    }//GEN-LAST:event_courseNameFieldActionPerformed
    /**
     * @param args the command line arguments
     */
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton cancelButton;
        private javax.swing.JTextField courseNameField;
        private javax.swing.JLabel courseNameLabel;
        private javax.swing.JTextField creditsField;
        private javax.swing.JLabel creditsLabel;
        private javax.swing.JButton okButton;
        // End of variables declaration//GEN-END:variables

}
